# # -*- coding: utf-8 -*-
# """
# Created on Sat Dec  4 20:50:49 2021
#
# @author: admin
# """
#
# from keras.layers import LSTM, Dense
# from keras.models import Sequential
# from keras.models import load_model
# import keras.backend as K
# import matplotlib.pyplot as plt
# from sklearn.metrics import r2_score
# import pandas as pd
# import numpy as np
# import os
#
#
# def cnn_load_data(stock_code):
#     data = pd.read_excel('data.xlsx')
#     train_data = []
#     train_label = []
#     for index,row in data.iterrows():
#         train_cells = []
#         train_cells.append(row['horizontal'])
#         train_cells.append(row['vertical'])
#         train_cells.append(row['space'])
#         train_cells = np.array(train_cells, dtype='float')
#         train_data.append(train_cells)
#         train_label.append(row['close'])
#     train_data = train_data[:len(train_data)-1]
#     train_label = train_label[1:]
#     length = len(train_data)
#     length = round(length * 0.8)
#     test_data = train_data[length:]
#     test_label = train_label[length:]
#     train_data = train_data[:length]
#     train_label = train_label[:length]
#     train_data = np.array(train_data, dtype='float')
#     train_label = np.array(train_label, dtype='float')
#     test_data = np.array(test_data, dtype='float')
#     test_label = np.array(test_label, dtype='float')
#     #label = to_categorical(label,num_classes=class_num)
#     return train_data,train_label,test_data,test_label
#
# def lstm_load_data(stock_code):
#     '''file = Path('./tdxstocks/day/' + finance.get_tdx_type(stock_code) + '.xlsx')
#     if not file.exists():
#         tdxdata.get_tdx_day(finance.get_tdx_type(stock_code) + '.day')
#     data = pd.read_excel(file, index_col = 'date')'''
#     data = ts.get_hist_data(stock_code)
#     mf_data = trendline.mainforce_monitor_ml(data)
#     gs_data = trendline.golden_snipe_ml(data)
#     data = data[['open', 'high', 'close', 'low', 'ma5', 'ma10', 'ma20']]
#     data = data.iloc[:len(data) - 100]
#     data = pd.merge(data, mf_data, on='date')
#     data = pd.merge(data, gs_data, on='date')
#     data = data.iloc[::-1]
#     train_data = []
#     train_label = []
#     for index,row in data.iterrows():
#         train_cells = []
#         train_cells.append(row['horizontal'])
#         train_cells.append(row['vertical'])
#         train_cells.append(row['space'])
#         train_cells = np.array(train_cells, dtype='float')
#         train_data.append(train_cells)
#         train_label.append(row['close'])
#     train_data = train_data[:len(train_data)-1]
#     train_label = train_label[1:]
#     length = len(train_data)
#     length = round(length * 0.8)
#     test_data = train_data[length:]
#     test_label = train_label[length:]
#     train_data = train_data[:length]
#     train_label = train_label[:length]
#     train_data = np.array(train_data, dtype='float')
#     train_label = np.array(train_label, dtype='float')
#     test_data = np.array(test_data, dtype='float')
#     test_label = np.array(test_label, dtype='float')
#     train_data = train_data.reshape((train_data.shape[0], 1, train_data.shape[1]))
#     test_data = test_data.reshape((test_data.shape[0], 1, test_data.shape[1]))
#     #label = to_categorical(label,num_classes=class_num)
#     return train_data,train_label,test_data,test_label
#
# def r2(y_true, y_pred):
#     a = K.square(y_pred - y_true)
#     b = K.sum(a)
#     c = K.mean(y_true)
#     d = K.square(y_true - c)
#     e = K.sum(d)
#     f = 1 - b/e
#     return f
#
# class CNN:
#     def neural_model():
#         #input_shape = (bin_n * 4, 1)
#         model = Sequential()
#         model.add(Dense(64, activation='relu', input_dim=17))
#         model.add(Dense(64, activation='relu'))
#         model.add(Dense(64, activation='relu'))
#         model.add(Dense(64, activation='relu'))
#         model.add(Dense(64, activation='relu'))
#         model.add(Dense(64, activation='relu'))
#         model.add(Dense(64, activation='relu'))
#         model.add(Dense(64, activation='relu'))
#         model.add(Dense(64, activation='relu'))
#         model.add(Dense(64, activation='relu'))
#         model.add(Dense(1))
#         return model
#
# def cnn_train(stock_code):
#     batch_size = 32
#     epochs = 1000
#     model = CNN.neural_model()
#     train_x, train_y, test_x, test_y = cnn_load_data(stock_code)
#
#     model.compile(loss='mse', optimizer='rmsprop', metrics=['mae',r2])
#     model.fit(train_x, train_y, batch_size=batch_size, epochs=epochs, validation_split=0.2)
#
#     pred_test_y = model.predict(test_x)
#     #print(pred_test_y)
#
#     pred_acc = r2_score(test_y, pred_test_y)
#     print('pred_acc', pred_acc)
#
#     plt.rcParams['font.sans-serif'] = ['SimHei']
#     plt.rcParams['axes.unicode_minus'] = False
#
#     plt.figure(figsize=(8, 4), dpi=80)
#     plt.plot(range(len(test_y)), test_y, ls='-.',lw=2,c='r',label='真实值')
#     plt.plot(range(len(pred_test_y)), pred_test_y, ls='-',lw=2,c='b',label='预测值')
#
#     plt.grid(alpha=0.4, linestyle=':')
#     plt.legend()
#     plt.xlabel('number')
#     plt.ylabel('股价')
#
#     plt.show()
#
# def cnn_predict(stock_code, length):
#     '''file = Path('./tdxstocks/day/' + finance.get_tdx_type(stock_code) + '.xlsx')
#     if not file.exists():
#         tdxdata.get_tdx_day(finance.get_tdx_type(stock_code) + '.day')
#     data = pd.read_excel(file, index_col = 'date')'''
#     data = ts.get_hist_data(stock_code)
#     mf_data = trendline.mainforce_monitor_ml(data)
#     gs_data = trendline.golden_snipe_ml(data)
#     data = data[['open', 'high', 'close', 'low', 'ma5', 'ma10', 'ma20']]
#     data = data.iloc[:len(data) - 100]
#     data = pd.merge(data, mf_data, on='date')
#     data = pd.merge(data, gs_data, on='date')
#     data = data.iloc[::-1]
#     traindata = []
#     label = []
#     for index,row in data.iterrows():
#         train_cells = []
#         train_cells.append(row['horizontal'])
#         train_cells.append(row['vertical'])
#         train_cells.append(row['space'])
#         train_cells = np.array(train_cells, dtype='float')
#         traindata.append(train_cells)
#         label.append(row['close'])
#     traindata = traindata[:len(traindata)-1]
#     label = label[1:]
#     traindata = np.array(traindata, dtype='float')
#     #traindata = np.expand_dims(traindata, axis=2)
#     label = np.array(label, dtype='float')
#     #label = to_categorical(label,num_classes=class_num)
#
# class ml_LSTM:
#     def neural_model():
#         model = Sequential()
#         model.add(LSTM(50, return_sequences=True, input_shape=(1, 18)))
#         model.add(Dense(64, activation='relu'))
#         model.add(Dense(64, activation='relu'))
#         model.add(Dense(64, activation='relu'))
#         model.add(Dense(64, activation='relu'))
#         model.add(Dense(64, activation='relu'))
#         model.add(LSTM(50, return_sequences=True))
#         model.add(Dense(64, activation='relu'))
#         model.add(Dense(64, activation='relu'))
#         model.add(Dense(64, activation='relu'))
#         model.add(Dense(64, activation='relu'))
#         model.add(Dense(64, activation='relu'))
#         model.add(LSTM(50))
#         model.add(Dense(1))
#         return model
#
# def lstm_train(stock_code):
#     batch_size = 8
#     epochs = 1000
#     train_x, train_y, test_x, test_y = lstm_load_data(stock_code)
#     model = ml_LSTM.neural_model(train_x)
#     #model.compile(loss='mae', optimizer='adam')
#     model.compile(loss='mse', optimizer='rmsprop', metrics=['mae',r2])
#     #model.fit(train_x, train_y, batch_size=batch_size, epochs=epochs, validation_split=0.2)
#     history = model.fit(train_x, train_y, batch_size=batch_size, epochs=epochs, validation_split=0.2)
#
#     plt.figure()
#     plt.plot(history.history['loss'], label='train')
#     plt.plot(history.history['val_loss'], label='test')
#     plt.legend()
#     #plt.show()
#
#     pred_test_y = model.predict(test_x)
#     #print(pred_test_y)
#
#     pred_acc = r2_score(test_y, pred_test_y)
#     print(test_y)
#     print(pred_test_y)
#     print('pred_acc', pred_acc)
#
#     plt.rcParams['font.sans-serif'] = ['SimHei']
#     plt.rcParams['axes.unicode_minus'] = False
#
#     plt.figure(figsize=(8, 4), dpi=80)
#     plt.plot(range(len(test_y)), test_y, ls='-.',lw=2,c='r',label='真实值')
#     plt.plot(range(len(pred_test_y)), pred_test_y, ls='-',lw=2,c='b',label='预测值')
#
#     plt.grid(alpha=0.4, linestyle=':')
#     plt.legend()
#     plt.xlabel('number')
#     plt.ylabel('股价')
#
#     plt.show()
#
# def lstm_train_all():
#     listfile = os.listdir('./tdxstocks/day/')
#     batch_size = 32
#     epochs = 1
#     #model = ml_LSTM.neural_model()
#     #model.compile(loss='mse', optimizer='rmsprop', metrics=['mae',r2])
#     model = load_model('lstm_model.h5', custom_objects={'r2': r2})
#     for stock_code in listfile:
#         data = pd.read_excel('./tdxstocks/day/' + stock_code, index_col = 'date')
#         if len(data) < 250:
#             continue
#         data = data[:600]
#
#         data = data.iloc[::-1]
#         train_data = []
#         train_label = []
#         for index,row in data.iterrows():
#             train_cells = []
#             train_cells.append(row['horizontal'])
#             train_cells.append(row['vertical'])
#             train_cells.append(row['space'])
#             train_cells = np.array(train_cells, dtype='float')
#             train_data.append(train_cells)
#             train_label.append(row['close'])
#         train_data = train_data[:len(train_data)-1]
#         train_label = train_label[1:]
#         train_data = np.array(train_data, dtype='float')
#         train_label = np.array(train_label, dtype='float')
#         train_data = train_data.reshape((train_data.shape[0], 1, train_data.shape[1]))
#         #label = to_categorical(label,num_classes=class_num)
#
#         #model.compile(loss='mae', optimizer='adam')
#
#         model.fit(train_data, train_label, batch_size=batch_size, epochs=epochs)
#
#         stock_code = stock_code[2:8]
#         print(stock_code + ' finished')
#     model.save('lstm_model.h5')
#
# def lstm_train_300():
#     df = pd.read_excel('./tdxstocks/hs300.xlsx', index_col = 0, converters = {'code':str})
#     batch_size = 32
#     epochs = 1
#     model = ml_LSTM.neural_model()
#     model.compile(loss='mse', optimizer='rmsprop', metrics=['mae',r2])
#     #model = load_model('lstm_model.h5', custom_objects={'r2': r2})
#     for iters in range(0,30):
#         for index,row in df.iterrows():
#             stock_code = finance.get_tdx_type(row[0])
#             data = pd.read_excel('./tdxstocks/day/' + stock_code + '.xlsx', index_col = 'date')
#             if len(data) < 250:
#                 continue
#             data = data[:600]
#             mf_data = trendline.mainforce_monitor_ml(data)
#             gs_data = trendline.golden_snipe_ml(data)
#             ma5_data = trendline.ma(data, 5)
#             ma10_data = trendline.ma(data, 10)
#             ma20_data = trendline.ma(data, 20)
#             ma30_data = trendline.ma(data, 30)
#             data = data[['open', 'high', 'close', 'low']]
#             data = data.iloc[:len(data) - 100]
#             data = pd.merge(data, ma5_data, on='date')
#             data = pd.merge(data, ma10_data, on='date')
#             data = pd.merge(data, ma20_data, on='date')
#             data = pd.merge(data, ma30_data, on='date')
#             data = pd.merge(data, mf_data, on='date')
#             data = pd.merge(data, gs_data, on='date')
#             data = data.iloc[::-1]
#             train_data = []
#             train_label = []
#             for index,row in data.iterrows():
#                 train_cells = []
#                 train_cells.append(row['horizontal'])
#                 train_cells.append(row['vertical'])
#                 train_cells.append(row['space'])
#                 train_cells = np.array(train_cells, dtype='float')
#                 train_data.append(train_cells)
#                 train_label.append(row['close'])
#             train_data = train_data[:len(train_data)-1]
#             train_label = train_label[1:]
#             train_data = np.array(train_data, dtype='float')
#             train_label = np.array(train_label, dtype='float')
#             train_data = train_data.reshape((train_data.shape[0], 1, train_data.shape[1]))
#             #label = to_categorical(label,num_classes=class_num)
#
#             #model.compile(loss='mae', optimizer='adam')
#
#             model.fit(train_data, train_label, batch_size=batch_size, epochs=epochs)
#
#             stock_code = stock_code[2:8]
#             print(stock_code + ' iter' + str(iters) + ' finished')
#     model.save('lstm_300.h5')
#
# def lstm_train_500():
#     df = pd.read_excel('./tdxstocks/zz500.xlsx', index_col = 0, converters = {'code':str})
#     batch_size = 32
#     epochs = 1
#     model = ml_LSTM.neural_model()
#     model.compile(loss='mse', optimizer='rmsprop', metrics=['mae',r2])
#     #model = load_model('lstm_model.h5', custom_objects={'r2': r2})
#     for iters in range(0,30):
#         for index,row in df.iterrows():
#             data = data.iloc[::-1]
#             train_data = []
#             train_label = []
#             for index,row in data.iterrows():
#                 train_cells = []
#                 train_cells.append(row['horizontal'])
#                 train_cells.append(row['vertical'])
#                 train_cells.append(row['space'])
#                 train_cells = np.array(train_cells, dtype='float')
#                 train_data.append(train_cells)
#                 train_label.append(row['close'])
#             train_data = train_data[:len(train_data)-1]
#             train_label = train_label[1:]
#             train_data = np.array(train_data, dtype='float')
#             train_label = np.array(train_label, dtype='float')
#             train_data = train_data.reshape((train_data.shape[0], 1, train_data.shape[1]))
#             #label = to_categorical(label,num_classes=class_num)
#
#             #model.compile(loss='mae', optimizer='adam')
#
#             model.fit(train_data, train_label, batch_size=batch_size, epochs=epochs)
#
#             stock_code = stock_code[2:8]
#             print(stock_code + ' iter' + str(iters) + ' finished')
#     model.save('lstm_500.h5')
#
# def lstm_predict(stock_code):
#     train_data = []
#     train_label = []
#     for index,row in data.iterrows():
#         train_cells = []
#         train_cells.append(row['horizontal'])
#         train_cells.append(row['vertical'])
#         train_cells.append(row['space'])
#         train_cells = np.array(train_cells, dtype='float')
#         train_data.append(train_cells)
#         train_label.append(row['close'])
#     train_data = train_data[:len(train_data)-1]
#     train_label = train_label[1:]
#     test_x = np.array(train_data, dtype='float')
#     test_y = np.array(train_label, dtype='float')
#     test_x = test_x.reshape((test_x.shape[0], 1, test_x.shape[1]))
#     model = load_model('lstm_300.h5', custom_objects={'r2': r2})
#
#     pred_test_y = model.predict(test_x)
#     #print(pred_test_y)
#
#     pred_acc = r2_score(test_y, pred_test_y)
#     print(pred_acc)
#
#     plt.rcParams['font.sans-serif'] = ['SimHei']
#     plt.rcParams['axes.unicode_minus'] = False
#
#     plt.figure(figsize=(8, 4), dpi=80)
#     plt.plot(range(len(test_y)), test_y, ls='-.',lw=2,c='r',label='真实值')
#     plt.plot(range(len(pred_test_y)), pred_test_y, ls='-',lw=2,c='b',label='预测值')
#
#     plt.grid(alpha=0.4, linestyle=':')
#     plt.legend()
#     plt.xlabel('number')
#     plt.ylabel('股价')
#
#     plt.show()
#
#
# if __name__ == '__main__':
#     #lstm_train('002221')
#     #cnn_train('002221')
#     #predict_nextday('002221')
#     #load_data('002221')
#     #lstm_train_all()
#     #lstm_train_300()
#     lstm_predict('002221')